
import sqlalchemy
from fastapi import Request
from pydantic import BaseModel, Field

from .utils.get_config import config_data
from .api_model.response_model import ApiResponse
from .prediction_tools.class_group import group_for_class

STATIC_URL = config_data['static_path']


class ClassGroupModel(BaseModel):
    class_name: str = Field(..., min_length=1, max_length=255, description="班级名称")
    num_of_groups: int = Field(...,  description="分组人数")


async def class_group_api(request: Request, data: ClassGroupModel, engine):
    groups = group_for_class(data.class_name, data.num_of_groups, engine)
    if not groups:
        return ApiResponse(code=405,
                           data={
                               "class_name": data.class_name,
                               "num_of_groups": data.num_of_groups,
                               "result": "",
                               "img_path": ""
                           },
                           msg="分组失败，分组数量不能大于人数")
    group_list = []
    for group_id, members in groups.items():
        group_list.append(members)
    return ApiResponse(code=200,
                       data={
                           "class_name": data.class_name,
                           "num_of_groups": data.num_of_groups,
                           "result": group_list,
                           "img_path": ""
                       },
                       msg="分组成功")
